package com.akoj.anmu.controller;

import com.akoj.anmu.common.BaseResponse;
import com.akoj.anmu.common.ResponseCode;
import com.akoj.anmu.common.ResultUtils;
import com.akoj.anmu.exception.BusinessException;
import com.akoj.anmu.service.TeamService;
import com.akoj.anmu.service.UserService;
import com.akoj.model.domain.dto.team.*;
import com.akoj.model.domain.vo.LoginUserVO;
import com.akoj.model.domain.vo.TeamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 团队控制层
 *
 * @author: akme
 */
@RestController(value = "team")
@Api(tags = "团队模块")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class TeamController {

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    @ApiOperation(value = "创建团队")
    @PostMapping("/addTeam")
    public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request) {
        //判断参数是否为空
        if(teamAddRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        //判断是否登录
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.addTeam(teamAddRequest,currentUser));
    }

    @ApiOperation(value = "查询所有队伍")
    @PostMapping("/listAllTeam")
    public BaseResponse<List<TeamVO>> listAllTeam(@RequestBody TeamQueryRequest teamQueryRequest, HttpServletRequest request){
        //判断参数是否为空
        if(teamQueryRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        //判断是否登录
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.listAllTeam(teamQueryRequest,currentUser));
    }

    @ApiOperation(value = "修改队伍信息")
    @PostMapping("/updateTeam")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest, HttpServletRequest request){
        if(teamUpdateRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"参数为空");
        }
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.updateTeam(teamUpdateRequest,currentUser));
    }

    @ApiOperation(value = "加入队伍")
    @PostMapping("/joinTeam")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request){
        if(teamJoinRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"参数为空");
        }
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.joinTeam(teamJoinRequest,currentUser));
    }

    @ApiOperation(value = "退出队伍")
    @PostMapping("/quitTeam")
    public BaseResponse<Boolean> quitTeam(@RequestBody TeamQuitRequest teamQuitRequest, HttpServletRequest request){
        if(teamQuitRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"参数为空");
        }
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.quitTeam(teamQuitRequest,currentUser));
    }

    @ApiOperation(value = "解散队伍")
    @PostMapping("/deleteTeam")
    public BaseResponse<Boolean> deleteTeam(@RequestBody TeamDeleteRequest teamDeleteRequest, HttpServletRequest request){
        if(teamDeleteRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR,"参数为空");
        }
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.deleteTeam(teamDeleteRequest,currentUser));
    }

    @ApiOperation(value = "根据队伍ID获取队伍")
    @PostMapping("/getTeamById")
    public BaseResponse<TeamVO> getTeamById(@RequestBody TeamQueryRequest teamQueryRequest, HttpServletRequest request){
        //判断参数是否为空
        if(teamQueryRequest == null){
            throw new BusinessException(ResponseCode.PARAMS_ERROR, "参数为空");
        }
        //判断是否登录
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.getTeamById(teamQueryRequest,currentUser));
    }

    @ApiOperation(value = "查询该用户创建的队伍")
    @PostMapping("/userCreateTeam")
    public BaseResponse<List<TeamVO>> listUserCreateTeam(HttpServletRequest request){
        //判断是否登录
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.listUserCreateTeam(currentUser));
    }

    @ApiOperation(value = "查询用户加入的队伍")
    @PostMapping("/userJoinTeam")
    public BaseResponse<List<TeamVO>> listUserJoinTeam(HttpServletRequest request){
        //判断是否登录
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.listUserJoinTeam(currentUser));
    }

    @ApiOperation(value = "查询用户未加入的队伍")
    @PostMapping("/userNotJoinTeam")
    public BaseResponse<List<TeamVO>> listUserNotJoinTeam(HttpServletRequest request){
        //判断是否登录
        LoginUserVO currentUser = userService.getCurrentUser(request);
        return ResultUtils.success(teamService.listUserNotJoinTeam(currentUser));
    }




}
